package com.app.simpsonux;

import com.app.simpsonux.R;

import android.app.*;

import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.Intent;
import android.content.SharedPreferences;

public class SimpsonUX extends Activity
implements View.OnClickListener
{
    Button btnEnter;
    Button btnRegist;
    
    private String mUserId = "";
    private String mUserPasswd = "";
    private String mSipServerAddr = "";
    private int    mSipServerPort = 0;
   
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.simpsonux );
        
        btnEnter = (Button) findViewById( R.id.buttonEnter );
        btnEnter.setOnClickListener( this );
        btnRegist = (Button) findViewById( R.id.buttonRegist );
        btnRegist.setOnClickListener( this );
    }

    @Override
    public boolean onCreateOptionsMenu( Menu menu )
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate( R.menu.linear_layout, menu );
        return true;
    }
    
    public String getUserId()
    {
        return mUserId;
    }

    public void onClick( View v )
    {
        Intent   sIntent = null;
        UXParcel sParcel = null;
        switch( v.getId() )
        {
        case R.id.buttonEnter:
            mUserId = ( (EditText)findViewById( R.id.editMainUserId ) )
                      .getText().toString();
            mUserPasswd = ( (EditText)findViewById( R.id.editMainUserPasswd ) )
                          .getText().toString();
            if( checkUserID( mUserId, mUserPasswd ) == true )
            {
                sIntent = new Intent( SimpsonUX.this, DialerViewActivity.class );
                sParcel = new UXParcel();
                sParcel.mUserId     = mUserId;
                sParcel.mUserPwd    = mUserPasswd;
                sParcel.mServerAddr = mSipServerAddr;
                sParcel.mServerPort = mSipServerPort;
                sIntent.putExtra( this.getString(R.string.intent_userinfo ),
                                  sParcel );
                startActivity( sIntent );
            }
            break;
        case R.id.buttonRegist:
            sIntent = new Intent( SimpsonUX.this,
                                  RegistViewActivity.class );
            startActivity( sIntent );
            break;
        default:
            break;
        }
    }
    
    public boolean checkUserID( String aUserId, String aUserPasswd )
    {
        String sUserId     = "";
        String sUserPasswd = "";
        
        SharedPreferences sPreference = getSharedPreferences(
                                           aUserId,
                                           MODE_PRIVATE );
        if( sPreference != null )
        {
            sUserId = sPreference.getString(
                               getString( R.string.pref_field_user_id ),
                               "" );
            if( aUserId.compareTo( sUserId ) == 0 )
            {
                sUserPasswd = sPreference.getString(
                                       getString( 
                                            R.string.pref_field_user_password ),
                                            "" );
                if( sUserPasswd.length() == 0 && 
                        aUserPasswd.length() == 0 )
                {
                    mSipServerAddr = sPreference.getString(
                                       getString(
                                            R.string.pref_field_server_addr ),
                                            "" );
                    mSipServerPort = sPreference.getInt(
                    		           getString( R.string.pref_field_server_port ),
                    		           5060 );
                    return true;
                }
                else
                {
                    if( aUserPasswd.compareTo( sUserPasswd ) == 0 )
                    {
                        mSipServerAddr = sPreference.getString(
                                           getString(R.string.pref_field_server_addr ),
                                           "" );                        
                        return true;
                    }
                    else
                    {
                        UXUt.AlertDlg( this,
                                       toString(),
                                       getString( R.string.error_illegal_password ),
                                       false );
                    }
                }
            }
            else
            {
                UXUt.AlertDlg( this,
                               toString(),
                               getString( R.string.error_not_found_user ),
                               false );
            }
        }
        else
        {
            UXUt.AlertDlg( this,
                           toString(),
                           getString( R.string.error_not_found_user ),
                           false );        
        }
        return false;
    }
}